package top.wangjiu.java.jvm.constantinfo.literal;

import top.wangjiu.java.jvm.constantinfo.ConstantPoolInfo;
import top.wangjiu.java.jvm.constantinfo.LiteralConstantPoolInfo;

/**
 * @ClassName Utf8Info
 * @Description
 * @Author wangjiu
 * @Date 2022/1/14 11:56 上午
 */
public class Utf8CPInfo extends LiteralConstantPoolInfo {

    public final static short TAG = 1;
    private final short index;

    private final short length;

    private final byte[] bytes;
    private final String value;

    public Utf8CPInfo(byte[] bytes, short length, short index){
        this.index = index;
        this.length = length;
        this.bytes = bytes;
        this.value = new String(bytes);
    }

    @Override
    public short tag() {
        return TAG;
    }

    @Override
    public short index() {
        return index;
    }

    @Override
    public short infoLength() {
        return (short) (length + 2);
    }

    @Override
    public String toFullString(ConstantPoolInfo[] poolInfos) {
        return "#"+index +" = Utf8" + "\t\t"+value;
    }

    @Override
    public String toString(ConstantPoolInfo[] poolInfos) {
        return value;
    }
}
